//
//  WPModifyPasswordVC.m
//  wobingwoyi
//
//  Created by WGP on 16/8/8.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPModifyPasswordVC.h"
#import "WLCaptcheButton.h"
#import "NSString+Hash.h"

@interface WPModifyPasswordVC ()

@property (weak, nonatomic) IBOutlet UITextField *oldPswField;
@property (weak, nonatomic) IBOutlet UITextField *myNewPswField;
@property (weak, nonatomic) IBOutlet UITextField *sureNewPswField;
@property (weak, nonatomic) IBOutlet WLCaptcheButton *getCodeBtn;
@property(nonatomic,assign)BOOL isSending;

@end

@implementation WPModifyPasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
    WPUserAccount *account = [WPUserAccountViewModel sharedInstance].userAccount;
    self.oldPswField.text = account.user.phone;
    self.oldPswField.enabled = NO;
}
- (IBAction)captcheButtonDIdClick:(WLCaptcheButton *)sender
{
    if (self.isSending)
    {
        return ;
    }
//    NSLog(@"发送验证码");
    
    self.isSending = YES;
    
    NSString *url = @"user/sendMessage.do";
    NSDictionary *parameters = @{@"telephone":self.oldPswField.text};
    
    [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if ([responseObject[@"result"] isEqualToString:@"success"])
        {
            [self.getCodeBtn fire];
            [self showSuccessInfoWithStatus:@"验证码已发送至手机"];
        }
        else
        {
//            NSLog(@"%@",responseObject[@"detail"]);
        }
        self.isSending = NO;
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error)
        {
            [self showErrorInfoWithStatus:@"网络错误"];
        }
        self.isSending = NO;
    }];
}

- (IBAction)commitBtnDidClick
{
    if (!self.myNewPswField.text.length)
    {
        [self showErrorInfoWithStatus:@"请输入验证码"];
    }
    if (self.sureNewPswField.text.length >= 6 && self.sureNewPswField.text.length <= 20)
    {
        //    提交后台修改密码
        NSString *url = @"user/changePwd.do";
        NSDictionary *dict = @{@"account":self.oldPswField.text,@"newPwd":[self.sureNewPswField.text md5String],@"codeInput":self.myNewPswField.text};
        
        [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            if ([responseObject[@"result"] isEqualToString:@"success"])
            {
                [self showSuccessInfoWithStatus:@"修改成功"];
                //        用户退出登录的时候取消极光别名
                [JPUSHService setAlias:@"" callbackSelector:nil object:self];
                
                NSFileManager *defaultManager = [NSFileManager defaultManager];
                if([defaultManager removeItemAtPath:filepath error:NULL])
                {
                    WPLog(@"删除归档文件成功");
                }
                [defaultManager removeItemAtPath:MRFilePath error:NULL];
                
                [WPUserAccountViewModel sharedInstance].userAccount = nil;
                
                [(AppDelegate *)([UIApplication sharedApplication].delegate) switchController:[WPTabBarController new]];
            }
            else
            {
                //            考虑到账号过期的情况
                if ([responseObject[@"detail"] isEqualToString:@"illegalLogin"])
                {
                    //                账号过期
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"账号过期,请重新登录" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                    [alertView show];
                }
                else
                {
                    [self showErrorInfoWithStatus:@"修改失败"];
                }
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
        
    }
    else
    {
        [self showErrorInfoWithStatus:@"请输入6~20位密码"];
    }
}

- (IBAction)back
{
    [self.navigationController popViewControllerAnimated:YES];
}


@end
